package com.memory.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.api.service.OrderService;
import com.memory.common.common.BaseResponse;
import com.memory.common.common.ErrorCode;
import com.memory.common.common.ResultUtils;
import com.memory.common.exception.BusinessException;
import com.memory.common.exception.ThrowUtils;
import com.memory.common.model.dto.order.OrderQueryRequest;
import com.memory.common.model.entity.Order;
import com.memory.common.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ClassName: OrderController
 * Package: com.memory.api.controller
 * Description: 订单接口
 *
 * @Author Memory
 * @Create 2024/6/13 21:45
 * @Version 1.0
 */

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 充值
     *
     * @param accessKey
     * @return
     */
    @PostMapping("/recharge")
    public User doRecharge(@RequestParam(value = "accessKey") String accessKey) {
        return null;
        // 判断用户是否存在
        // 判断充值活动是否存在

        // 充值

        // 用户积分增加
        // 充值成功
    }

    /**
     * 获取订单信息列表
     * 分页查询
     *
     * @param oderQueryRequest 查询参数
     * @return 接口信息列表
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Order>> listInterfaceInfoByPage(@RequestBody OrderQueryRequest oderQueryRequest) {
        // controller校验参数
        if (oderQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        long current = oderQueryRequest.getCurrent();
        long size = oderQueryRequest.getPageSize();

        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<Order> orderPage = orderService.page(new Page<>(current, size));
        return ResultUtils.success(orderPage);
    }
}
